
# =============================================================
# File: App_C_clustered.R
# Purpose: Produces results from Appendix Section C: Clustered Standard Errors
# Paper: Foreignness as an Asset: European Carbon Regulation and the Relocation Threat among Multinational Firms
# Author: Patrick Bayer, patrick.bayer@strath.ac.uk
# Date: 5 October 2022
#
# Data: ./data.csv
#
# Technical disclaimer:
# All analyses in R version 4.1.2 (2021-11-01)
# RStudio 2022.07.1 Build 554 ("Spotted Wakerobin" Release (7872775e, 2022-07-22) for Windows)
# Windows 10 Enterprise, 64-bit
# 12th Gen Intel(R) Core(TM) i7-1270P 2.20 GHz with 32GB RAM
# =============================================================

library(lmtest)
library(sandwich)
library(MASS)
library(MatchIt)
library(cem)
library(cobalt)
library(cowplot)
library(ggpubr)
library(marginaleffects)
library(AER)
library(tidyverse)

# Load data
df <- read_csv(file = "./data.csv")

# Trim sample down to true within firm sample
# (1) Limit sample to European MNCs: no allocation data for domestically-owned operations for non-EU MNCs
# (2) Exclude MNCs with only foreign-owned operations as there is no within MNC variation

df$sample <- ifelse(df$mnc.eu==1 & df$foreign.share<1,1,0)
df <- df[df$sample==1,]
df$foreign <- as.factor(df$foreign) # set variable as categorical

# =============================================================
# APPENDIX C: Clustered standard errors
# =============================================================

# Re-estimate the main results with different types of clustered standard errors

# Create indicator variable for two-way clusters
df$ctry_bvd <- paste(df$iso2,df$BVD) # Country-firm cluster
df$ctry_sector <- paste(df$iso2,df$ETSsector) # Country-sector cluster
df$sector_bvd <- paste(df$ETSsector,df$BVD) # Sector-firm cluster
df$ctry_bvd_sector <- paste(df$iso2,df$BVD,df$ETSsector) # Country-firm-sector cluster

# =============================================================
# Model (1)
# =============================================================

m1 <- lm(logDV~foreign+logAlloc0+logEmit0+as.factor(iso2)+as.factor(BVD), data=df)

# Marginal effects
(exp(summary(m1)$coef[["foreign1","Estimate"]])-1)*100

# Standard errors
(exp(coefci(m1, vcov. = vcovHC, type="HC0")["foreign1",])-1)*100 # robust SEs

(exp(coefci(m1, vcov. = vcovCL, cluster=~iso2)["foreign1",])-1)*100 # SEs clustered at country-level
(exp(coefci(m1, vcov. = vcovCL, cluster=~ETSsector)["foreign1",])-1)*100 # SEs clustered at sector-level
(exp(coefci(m1, vcov. = vcovCL, cluster=~BVD)["foreign1",])-1)*100 # SEs clustered at firm-level

# Two-way clustered SE
(exp(coefci(m1, vcov. = vcovCL, cluster=~ctry_sector)["foreign1",])-1)*100 # SEs clustered at country-sector level
(exp(coefci(m1, vcov. = vcovCL, cluster=~ctry_bvd)["foreign1",])-1)*100 # SEs clustered at country-firm level
(exp(coefci(m1, vcov. = vcovCL, cluster=~sector_bvd)["foreign1",])-1)*100 # SEs clustered at sector-firm level

# Three-way clustered SE
(exp(coefci(m1, vcov. = vcovCL, cluster=~ctry_bvd_sector)["foreign1",])-1)*100 # SEs clustered at country-sector-firm level

# =============================================================
# Model (2)
# =============================================================

m2 <- lm(logDV~foreign+logAlloc0+logEmit0+as.factor(iso2)+as.factor(ETSsector)+as.factor(BVD), data=df)

# Marginal effects
(exp(summary(m2)$coef[["foreign1","Estimate"]])-1)*100

# Standard errors
(exp(coefci(m2, vcov. = vcovHC, type="HC0")["foreign1",])-1)*100 # robust SEs

(exp(coefci(m2, vcov. = vcovCL, cluster=~iso2)["foreign1",])-1)*100 # SEs clustered at country-level
(exp(coefci(m2, vcov. = vcovCL, cluster=~ETSsector)["foreign1",])-1)*100 # SEs clustered at sector-level
(exp(coefci(m2, vcov. = vcovCL, cluster=~BVD)["foreign1",])-1)*100 # SEs clustered at firm-level

# Two-way clustered SEs
(exp(coefci(m2, vcov. = vcovCL, cluster=~ctry_sector)["foreign1",])-1)*100 # SEs clustered at country-sector level
(exp(coefci(m2, vcov. = vcovCL, cluster=~ctry_bvd)["foreign1",])-1)*100 # SEs clustered at country-firm level
(exp(coefci(m2, vcov. = vcovCL, cluster=~sector_bvd)["foreign1",])-1)*100 # SEs clustered at sector-firm level

# Three-way clustered SEs
(exp(coefci(m2, vcov. = vcovCL, cluster=~ctry_bvd_sector)["foreign1",])-1)*100 # SEs clustered at country-sector-firm level

# =============================================================
# Model (3)
# =============================================================

# Exact matching on 4-digit NACE code and firm
df$NACE <- ifelse(df$NACE=="",NA,df$NACE) # Recode empty strings as NA 

m <- matchit(foreign ~ NACE + BVD, data=df[is.na(df$NACE)==FALSE,], method="exact")
df.match <- match.data(m)

# Re-run main model on matched sample
m3 <- lm(logDV~foreign+logAlloc0+logEmit0+as.factor(iso2)+as.factor(ETSsector)+as.factor(BVD), data=df.match, weights=weights)

# Marginal effects
(exp(summary(m3)$coef[["foreign1","Estimate"]])-1)*100

# Standard errors
(exp(coefci(m3, vcov. = vcovHC, type="HC0")["foreign1",])-1)*100 # robust SEs

(exp(coefci(m3, vcov. = vcovCL, cluster=~iso2)["foreign1",])-1)*100 # SEs clustered at country-level
(exp(coefci(m3, vcov. = vcovCL, cluster=~ETSsector)["foreign1",])-1)*100 # SEs clustered at sector-level
(exp(coefci(m3, vcov. = vcovCL, cluster=~BVD)["foreign1",])-1)*100 # SEs clustered at firm-level

# Two-way clustered SEs
(exp(coefci(m3, vcov. = vcovCL, cluster=~ctry_sector)["foreign1",])-1)*100 # SEs clustered at country-sector level
(exp(coefci(m3, vcov. = vcovCL, cluster=~ctry_bvd)["foreign1",])-1)*100 # SEs clustered at country-firm level
(exp(coefci(m3, vcov. = vcovCL, cluster=~sector_bvd)["foreign1",])-1)*100 # SEs clustered at sector-firm level

# Three-way clustered SEs
(exp(coefci(m3, vcov. = vcovCL, cluster=~ctry_bvd_sector)["foreign1",])-1)*100 # SEs clustered at country-sector-firm level


# =============================================================
#                       END OF CODE
# =============================================================